// car/index/index.js
import Toast from '../../ui/toast/index'
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pic_url:app.base_url,
        list:[],
        order_info_show:false,//订单明细
        cat_active:true,
        car_goods_num:0,//购物车商品数量
        total_price:0,//支付总价
        hd_price:[],//总的优惠
        order_else:[],
        is_ck_all:true,//全选
        ck_arr:[],//选择内容
        address:{},
        send_time:1,//配送时长
        order:{
            goods:[],//选择的商品列表
            address:{},//地址
            order_info:"",//备注
            order_else:{},//运费，包装费等  
            coupon_id:0,//优惠券
        },
        
    },

    

    //获取备注
    get_bz(e){
        let value = e.detail.value;
        this.data.order.order_info = value;
        console.log(this.data.order.order_info);
    },

    //更改明细
    change_order_info(){
        this.setData({
            order_info_show:!this.data.order_info_show,
        });
    },

    //更新购物车数量
   refresh_car(){
    let goods = wx.getStorageSync('goods');
    let num=0;
    if(goods){
        num=goods.length;
    }
    this.setData({
        car_goods_num:num,
    });
    },

    //获取商品信息
    get_goods(arr=[]){
        
        let goods = wx.getStorageSync('goods');
        let address = wx.getStorageSync('address');
        app.login.request("index.php?r=api/car/goods",{goods:goods,address:address}).then((res)=>{
            console.log(res);
            if(res.data.result){
                //处理相同的优惠价格
                let hd=res.data.hd_price;
                let arr=[];
                let hd_arr=[];
                for(let i=0;i<hd.length;i++){
                    if(arr.includes(hd[i].name)){
                        for(let j=0;j<hd_arr.length;j++){
                            if(hd_arr[j].name==hd[i].name){
                                hd_arr[j].price+=hd[i].name;
                            }
                        }
                    }else{
                        arr.push(hd[i].name);
                        let p={};
                        p.name = hd[i].name;
                        p.price=hd[i].price;
                        hd_arr.push(p);
                    }
                }
                this.setData({
                    list:res.data.data,
                    total_price:res.data.total_price,
                    hd_price:res.data.hd_price,
                    order_else:res.data.order_else,
                    send_time:res.data.send_time,
                });
                this.data.order.order_else=res.data.order_else;
            }else{
                Toast({
                    context:this,
                    selector:"#toast",
                    message:res.data.data,
                });
                this.setData({
                    list:[],
                    total_price:0,//支付总价
                    hd_price:[],//总的优惠
                    order_else:{}
                });
            }
        });
    },

    //选择商品
    onChange(e){
        let value = e.detail.value;
        let goods = wx.getStorageSync('goods');
        for(let i=0;i<goods.length;i++){
            if(value.includes(i)){
                goods[i].ck=1;
            }else{
                goods[i].ck=0;
            }
        }
        wx.setStorageSync('goods', goods);
        this.get_goods();
        this.setData({
            ck_arr:value,
        });
    },

    //全选
    ck_all(e){
        if(this.data.ck_arr.length>0){
            this.change_store_goods(0);
            this.data.ck_arr=[];
            this.setData({
                ck_arr:this.data.ck_arr,
            });
            this.get_goods();
            return;
        }
        let list = this.data.list;
        for(let i=0;i<list.length;i++){
            if(list[i].name!="error"){
                for(let j=0;j<list[i].list.length;j++){
                    this.data.ck_arr.push(list[i].list[j].ck);
                }
            }
        }

        this.setData({
            ck_arr:this.data.ck_arr,
        });
        this.change_store_goods(1);
        this.get_goods();
    },

    change_store_goods(is){
        let goods = wx.getStorageSync('goods');
        for(let i=0;i<goods.length;i++){
            if(this.data.ck_arr.includes(i)){
                goods[i].ck=is;
            }
        }
        wx.setStorageSync('goods', goods);
    },

    //数量
    change_num(e){
        let index = e.currentTarget.dataset.index;
        let value = e.detail.value;
        let goods = wx.getStorageSync('goods');
        if(value<=0){//处理删除
            wx.showModal({
              title: '提示',
              content: '确定要删除么',
              complete: (res) => {
                if (res.cancel) {
                  
                }
                if (res.confirm) {
                  goods.splice(index,1);
                  wx.setStorageSync('goods', goods);
                  this.get_goods();
                }
              }
            })

        }else{
            goods[index].num=value;
            wx.setStorageSync('goods', goods);
            this.get_goods();
        }
        
    },

    //结算
    to_order(){
        if(this.data.ck_arr.length<=0){
            Toast({
                context:this,
                selector:"#toast",
                message:"请选择商品",
            });
            return;
        }
        //判断地址
        let address = wx.getStorageSync('address');
        if(!address){
            Toast({
                context:this,
                selector:"#toast",
                message:"请填写收货地址",
            });
            return;
        }
        //获取购物车信息
        let goods = wx.getStorageSync('goods');
        let goods_arr=[];
        for(let i=0;i<goods.length;i++){
            if(this.data.ck_arr.includes(i)){
                goods_arr.push(goods[i]);
            }
        }
        this.data.order.goods = goods_arr;
        this.data.order.address_id = address;
        //生成订单
        app.login.request("index.php?r=api/car/order",{order:this.data.order}).then((res)=>{
            console.log(res);
            if(res.data.result){
                //清除购物车
                for(let i=0;i<goods.length;i++){
                    if(this.data.ck_arr.includes(i)){
                        goods.splice(i,1);
                    }
                }
                wx.setStorageSync('goods', goods);
                wx.redirectTo({
                  url: '/order/index/index?id='+res.data.data,
                })
            }else{
                
                wx.showModal({
                  title: '提示',
                  content: res.data.data,
                  showCancel:false,
                  complete: (res) => {
                
                    if (res.confirm) {
                        for(let i=0;i<goods.length;i++){
                            goods[i].ck=0;
                        }
                        wx.setStorageSync('goods', goods);
                        this.setData({
                            ck_arr:[],
                        });
                    }
                  }
                })
            }
        });
       
    },

    //添加地址
    to_address(){
        wx.navigateTo({
          url: '/car/addresslist/list',
        })
    },

    //获取地址
    get_address(){
        let id = wx.getStorageSync('address');
        app.login.request("index.php?r=api/address/getaddress",{id:id}).then((res)=>{
            console.log(res);
            if(res.data.result){
                this.setData({
                    address:res.data.data,
                });
                this.data.order.address=res.data.data;
            }
        });
    },

    //删除无效商品
    del_godos(e){
        let index = e.currentTarget.dataset.index;
        let goods = wx.getStorageSync('goods');
        goods.splice(index,1);
        wx.setStorageSync('goods', goods);
        this.get_goods();
    },

    //清空购物车
    clear_car(){
        wx.showModal({
          title: '提示',
          content: '确定要清空购物车么',
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
                wx.setStorageSync('goods', []);
                this.get_goods();
            }
          }
        })
    },

    //跳转活动页
    to_hd_list(e){
        let id = e.currentTarget.dataset.id;
        let type = e.currentTarget.dataset.type;
        wx.navigateTo({
          url: '/goods/hdlist/list?id='+id+"&type="+type,
        })
    },

    //跳转首页
    to_home(){
        wx.redirectTo({
          url: '/pages/index/index',
        })
    },

    //跳转分类
    to_cate(){
        wx.redirectTo({
          url: '/cate/index/index',
        })
    },

    //跳转我的
    to_mine(){
        wx.redirectTo({
          url: '/mine/index/index',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        app.get_name();
        this.get_goods();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.get_address();
        this.refresh_car();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        let goods = wx.getStorageSync('goods');
        for(let i=0;i<goods.length;i++){
            goods[i].ck=0;
        }
        wx.setStorageSync('goods', goods);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})